<script setup lang="ts">
import { watchEffect } from 'vue'
import { UserControllerService } from '../generated'
import { useUserStore } from '@/stores/user'
import { useRouter } from 'vue-router'

const user = useUserStore()
const router = useRouter()

import { provide, nextTick, ref } from 'vue'

const isRouterActive = ref(true)

provide('reload', () => {
  isRouterActive.value = false
  nextTick(() => {
    isRouterActive.value = true
  })
})

watchEffect(async () => {
  const res = await UserControllerService.getLoginUserUsingGet()

  if (res.code !== 0) {
    //1.删除user
    user.clearUser()
    //2.删除hasLogin，删除user
    localStorage.removeItem('hasLogin')
    localStorage.removeItem('user')
    //3.退出
    await UserControllerService.userLogoutUsingPost()
    //4.跳转
    router.push({
      path: '/login'
    })
  }
})
</script>

<template>
  <el-config-provider size="default" :z-index="3000" :locale="zh as any">
    <RouterView v-if="isRouterActive" />
  </el-config-provider>
</template>

<style>
@import './assets/css/main.css';
@import './assets/css/color-dark.scss';
</style>
